/*
	游戏内通用函数
*/
// 创建新纪录
Game.createRecord=function(id){
	var record={
		"player":
				{
					"name":"gamePlayer",
					"lv":1,
					"gold":1000,
					"star":5
				},
		"have_record":1,
		"stage":
				{
					"1":0,
					"2":-1,
					"3":-1,
					"4":-1,
					"5":-1
				},
		"character_unlock":
				{
					"1":1,
					"2":0,
					"3":0,
					"4":0,
					"5":0,
					"6":0,
					"7":0,
					"8":0,
					"9":0,
					"10":0,
					"11":0
				},
		"character":
				{
					"0":{
						"1":1,
						"2":0,
						"3":0,
						"4":0
					},
					"1":{
						"1":0,
						"2":0,
						"3":0,
						"4":0,
						"5":0,
						"6":0
					}
				},
		"have_equip":
				{
					1:[0,0],
					2:[0,0],
					3:[0,0],
					4:[0,0],
					5:[0,0],
					6:[0,0],
					7:[0,0],
					8:[0,0],
					9:[0,0],
					10:[0,0],
					11:[0,0],
					12:[0,0],
					13:[0,0],
					14:[0,0],
					15:[0,0],
					16:[0,0],
					17:[0,0],
					18:[0,0],
					19:[0,0],
					20:[0,0]
				},
		"character_skill":
				{
					"1":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"2":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"3":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"4":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"5":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"6":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"7":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"8":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"9":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"10":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					},
					"11":{
						"1":[0,0],
						"2":[0,0],
						"3":[0,0],
						"4":[0,0],
						"5":[0,0],
						"6":[0,0],
						"7":[0,0],
						"8":[0,0]
					}
				}
	}
	
	Game.gameRecord[id]=record;
}

// 删除一个记录
Game.deleteRecord=function(id){
	var record={
		"have_record":0
	};

	Game.gameRecord[id]=record;
}

// 随机商店物品
Game.randomShop=function(){
	for(var j=1;j<=10;j++){
        var type=Math.floor(Math.random()*6+1);
        var equipId=Math.floor(Math.random()*5+1);
        // 记录到商店列表
        Game.gameShopItems[j]=[type,equipId];
    }
}

// 往商店内添加一个物品
Game.shopAddItem=function(recordId, itemType, itemId){
	var addSuccess=false;
	for(var i=1;i<=10;i++){
		if(Game.gameShopItems[i][0]==0 && Game.gameShopItems[i][1]==0){
			Game.gameShopItems[i]=[itemType, itemId];
			addSuccess=true;
			break;
		}
	}
	if(!addSuccess){
		console.log("The shop is full, can't add item any more");
	}
	return addSuccess;
}

// 从商店删除一个物品
Game.shopDelItem=function(recordId, itemIndex){
	for(var i=itemIndex;i<=10;i++){
		if(i<10){
			Game.gameShopItems[i]=Game.gameShopItems[i+1];
		}
		if(i==10){
			Game.gameShopItems[i]=[0,0];
		}
	}
}

// 往背包添加一个物品
Game.bagAddItem=function(recordId, itemType, itemId){
	var addSuccess=false;
	for(var i=1;i<=20;i++){
		if(Game.gameRecord[Game.currRecord]["have_equip"][i][0]==0 && Game.gameRecord[Game.currRecord]["have_equip"][i][1]==0){
			Game.gameRecord[Game.currRecord]["have_equip"][i]=[itemType, itemId];
			addSuccess=true;
			break;
		}
	}
	if(!addSuccess){
		console.log("The bag is full, can't add item any more");
	}
	return addSuccess;
}

// 从背包删除一个物品
Game.bagDelItem=function(recordId, itemIndex){
	for(var i=itemIndex;i<=20;i++){
		if(i<20){
			Game.gameRecord[Game.currRecord]["have_equip"][i]=Game.gameRecord[Game.currRecord]["have_equip"][i+1];
		}
		if(i==20){
			Game.gameRecord[Game.currRecord]["have_equip"][i]=[0,0];
		}
	}
}

// 增加一个角色到队伍中
Game.teamAddCharacter=function(recordId, characterId){
	var addSuccess=false;
	for(var i=1;i<=4;i++){
		if(Game.gameRecord[Game.currRecord]["character"][0][i]==0){
			Game.gameRecord[Game.currRecord]["character"][0][i]=characterId;
			addSuccess=true;
			break;
		}
	}
	if(!addSuccess){
		console.log("The team is full, can't add character any more");
	}
	return addSuccess;
}

// 从队伍中去除一个角色
Game.teamDelCharacter=function(recordId, teamIndex){
	for(var i=teamIndex;i<=4;i++){
		if(i<4){
			Game.gameRecord[Game.currRecord]["character"][0][i]=Game.gameRecord[Game.currRecord]["character"][0][i+1];
		}
		if(i==4){
			Game.gameRecord[Game.currRecord]["character"][0][i]=0;
		}
	}
}

// 为角色装备一个物品
Game.roleEquipItem=function(recordId, roleId, itemType, itemId, itemIndexInBag){
	// 人物该位置原先是否有装备
	if(Game.gameRecord[Game.currRecord]["character"][roleId][itemType]>0){
		// 原先有装备 卸下装备 装备上新的装备
		var oldEquipedItemId=Game.gameRecord[Game.currRecord]["character"][roleId][itemType];
		Game.gameRecord[Game.currRecord]["character"][roleId][itemType]=itemId;
		Game.gameRecord[Game.currRecord]["have_equip"][itemIndexInBag]=[itemType, oldEquipedItemId];
	}else{
		// 原先没有装备 装上新装备 删除背包里的装备
		Game.gameRecord[Game.currRecord]["character"][roleId][itemType]=itemId;
		Game.bagDelItem(Game.currRecord, itemIndexInBag);
	}
}

// 角色卸下一个物品
Game.roleUnequipItem=function(recordId, roleId , itemType){
	// 判断背包是否已满
	var addItemFlag=Game.bagAddItem(recordId, itemType, Game.gameRecord[Game.currRecord]["character"][roleId][itemType]);
	if(addItemFlag){
		Game.gameRecord[Game.currRecord]["character"][roleId][itemType]=0;
	}else{
		console.log("The bag is full, can't unequip role equipment");
	}
}

// 角色升级技能
Game.addSkillPoint=function(recordId, roleId, skillId){
	// 是否可加 return false 技能前置条件达到 并且处于激活状态
	if(Game.checkSkillCanAdd(recordId, roleId, skillId) && Game.checkLvCanUseSkill(recordId, roleId, skillId)){
		var currLevel=Game.gameRecord[Game.currRecord]["character_skill"][roleId][skillId][0];
		if(currLevel>=3){
			// 已满级
			return false;
		}else{
			// 加一级
			Game.gameRecord[Game.currRecord]["character_skill"][roleId][skillId][0]=currLevel+1;
			return true;
		}
	}
	return false;
}

// 技能点重置
Game.resetSkillPoint=function(recordId, roleId){
	for(var i=1;i<=8;i++){
		Game.gameRecord[Game.currRecord]["character_skill"][roleId][i]=[0,0];
	}
}

// 取消激活某个技能
Game.disableSkill=function(recordId, roleId, skillId){
	if(Game.checkSkillCanInactive(recordId, roleId, skillId)){
		Game.gameRecord[Game.currRecord]["character_skill"][roleId][skillId][1]=0;
		return true;
	}else{
		return false;
	}
}

// 激活某个技能
Game.enableSkill=function(recordId, roleId, skillId){
	if(Game.checkSkillCanActive(recordId, roleId, skillId) && Game.checkLvCanUseSkill(recordId, roleId, skillId)){
		var currActive=Game.gameRecord[Game.currRecord]["character_skill"][roleId][skillId][1];
		if(currActive==1){
			// 已激活
			return false;
		}else{
			// 激活该技能 并且将同一层其他技能设置为未激活
			switch(skillId){
				case 1:
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][1][1]=1;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][2][1]=0;
					break;
				case 2:
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][1][1]=0;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][2][1]=1;
					break;
				case 3:
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][3][1]=1;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][4][1]=0;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][5][1]=0;
					break;
				case 4:
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][3][1]=0;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][4][1]=1;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][5][1]=0;
					break;
				case 5:
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][3][1]=0;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][4][1]=0;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][5][1]=1;
					break;
				case 6:
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][6][1]=1;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][7][1]=0;
					break;
				case 7:
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][6][1]=0;
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][7][1]=1;
					break;
				case 8:
					Game.gameRecord[Game.currRecord]["character_skill"][roleId][8][1]=1;
					break;
			}
			return true;
		}
	}
	return false;
}

// 检查技能是否处于激活状态
Game.checkSkillIsActive=function(recordId, roleId, skillId){
	var isActive=false;
	if(Game.gameRecord[Game.currRecord]["character_skill"][roleId][skillId][1]==1){
		isActive=true;
	}
	return isActive;
}

// 检查是否允许加点(技能处于激活状态)
Game.checkSkillCanAdd=function(recordId, roleId, skillId){
	// 判断每个技能是否能添加
	return Game.checkSkillIsActive(recordId, roleId, skillId);
}

// 检查是否允许激活(前置技能已激活 & 前置技能等级>1 & 下一层的非自己的后置技能未激活)
Game.checkSkillCanActive=function(recordId, roleId, skillId){
	// 判断每个技能是否能添加
	var roleSkillInfo=Game.gameRecord[Game.currRecord]["character_skill"][roleId];
	var roleSkillLv_1=roleSkillInfo[1][0];
	var roleSkillLv_1_enable=roleSkillInfo[1][1];
	var roleSkillLv_2=roleSkillInfo[2][0];
	var roleSkillLv_2_enable=roleSkillInfo[2][1];
	var roleSkillLv_3=roleSkillInfo[3][0];
	var roleSkillLv_3_enable=roleSkillInfo[3][1];
	var roleSkillLv_4=roleSkillInfo[4][0];
	var roleSkillLv_4_enable=roleSkillInfo[4][1];
	var roleSkillLv_5=roleSkillInfo[5][0];
	var roleSkillLv_5_enable=roleSkillInfo[5][1];
	var roleSkillLv_6=roleSkillInfo[6][0];
	var roleSkillLv_6_enable=roleSkillInfo[6][1];
	var roleSkillLv_7=roleSkillInfo[7][0];
	var roleSkillLv_7_enable=roleSkillInfo[7][1];
	var roleSkillLv_8=roleSkillInfo[8][0];
	var roleSkillLv_8_enable=roleSkillInfo[8][1];

	var canAdd=false;
	switch(skillId){
		case 1:
			if(roleSkillLv_5_enable==0){
				canAdd=true;
			}
			break;
		case 2:
			if(roleSkillLv_3_enable==0){
				canAdd=true;
			}
			break;
		case 3:
			if(roleSkillLv_1>0 && roleSkillLv_1_enable>0 && roleSkillLv_7_enable==0){
				canAdd=true;
			}else{
				canAdd=false;
			}
			break;
		case 4:
			if((roleSkillLv_1>0 && roleSkillLv_1_enable>0)||(roleSkillLv_2>0 && roleSkillLv_2_enable>0)){
				canAdd=true;
			}else{
				canAdd=false;
			}
			break;
		case 5:
			if(roleSkillLv_2>0 && roleSkillLv_2_enable>0 && roleSkillLv_6_enable==0){
				canAdd=true;
			}else{
				canAdd=false;
			}
			break;
		case 6:
			if((roleSkillLv_3>0 && roleSkillLv_3_enable>0)||(roleSkillLv_4>0 && roleSkillLv_4_enable>0)){
				canAdd=true;
			}else{
				canAdd=false;
			}
			break;
		case 7:
			if((roleSkillLv_4>0 && roleSkillLv_4_enable>0)||(roleSkillLv_5>0 && roleSkillLv_5_enable>0)){
				canAdd=true;
			}else{
				canAdd=false;
			}
			break;
		case 8:
			if((roleSkillLv_6>0 && roleSkillLv_6_enable>0)||(roleSkillLv_7>0 && roleSkillLv_7_enable>0)){
				canAdd=true;
			}else{
				canAdd=false;
			}
			break;
	}

	return canAdd;
}

// 检查是否允许取消激活某个技能(后置技能处于非激活状态)
Game.checkSkillCanInactive=function(recordId, roleId, skillId){
	// 判断每个技能是否能添加
	var roleSkillInfo=Game.gameRecord[Game.currRecord]["character_skill"][roleId];
	var roleSkillLv_1=roleSkillInfo[1][0];
	var roleSkillLv_1_enable=roleSkillInfo[1][1];
	var roleSkillLv_2=roleSkillInfo[2][0];
	var roleSkillLv_2_enable=roleSkillInfo[2][1];
	var roleSkillLv_3=roleSkillInfo[3][0];
	var roleSkillLv_3_enable=roleSkillInfo[3][1];
	var roleSkillLv_4=roleSkillInfo[4][0];
	var roleSkillLv_4_enable=roleSkillInfo[4][1];
	var roleSkillLv_5=roleSkillInfo[5][0];
	var roleSkillLv_5_enable=roleSkillInfo[5][1];
	var roleSkillLv_6=roleSkillInfo[6][0];
	var roleSkillLv_6_enable=roleSkillInfo[6][1];
	var roleSkillLv_7=roleSkillInfo[7][0];
	var roleSkillLv_7_enable=roleSkillInfo[7][1];
	var roleSkillLv_8=roleSkillInfo[8][0];
	var roleSkillLv_8_enable=roleSkillInfo[8][1];

	var canAdd=false;
	switch(skillId){
		case 1:
			if(roleSkillLv_3_enable==0 && roleSkillLv_4_enable==0){
				canAdd=true;
			}
			break;
		case 2:
			if(roleSkillLv_4_enable==0 && roleSkillLv_5_enable==0){
				canAdd=true;
			}
			break;
		case 3:
			if(roleSkillLv_6_enable==0){
				canAdd=true;
			}
			break;
		case 4:
			if(roleSkillLv_6_enable==0 && roleSkillLv_7_enable==0){
				canAdd=true;
			}
			break;
		case 5:
			if(roleSkillLv_7_enable==0){
				canAdd=true;
			}
			break;
		case 6:
			if(roleSkillLv_8_enable==0){
				canAdd=true;
			}
			break;
		case 7:
			if(roleSkillLv_8_enable==0){
				canAdd=true;
			}
			break;
		case 8:
			canAdd=true;
			break;
	}

	return canAdd;
}

// 检查人物等级是否可以学习某个技能
Game.checkLvCanUseSkill=function(recordId, roleId, skillId){
	return true;
}

//*****************数组方法扩展***************************
// 数组中删除第n个元素
Array.prototype.del=function(n) {
	if(n<0 || n>=this.length){
		return this;
	}else{
		return this.slice(0,n).concat(this.slice(n+1,this.length));
	}
}
// 数组根据某属性排序
Array.prototype.sortByKey=function(key){
	for(var i=0;i<this.length-1;i++){
		for(var j=0;j<this.length-i-1;j++){
			if(this[j+1][key]<this[j][key]){
				var temp=this[j];
				this[j]=this[j+1];
				this[j+1]=temp;
			}
		}
	}
	return this;
}
Array.prototype.sortByKey2=function(key) {
	for(var i=0;i<this.length-1;i++){
		for(var j=i+1;j<this.length;j++){
			if(this[i][key]>this[j][key]){
				var temp=this[i];
				this[i]=this[j];
				this[j]=temp;
			}
		}
	}
	return this;
}

Array.prototype.indexOf = function(val) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == val) return i;
	}
	return -1;
};
Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if (index > -1) {
		this.splice(index, 1);
	}
};

Array.prototype.removeAllZero = function(key) {
	if(this.length<=0){
		return this;
	}
	var that=this.sortByKey2();
	that.reverse();
	for(var i=that.length-1;i>=0;i--){
		if(that[i][key]==0){
			that.pop();
		}else{
			break;
		}
	}
	return that;
};

Array.prototype.existKey = function(key,value){
	for(var i=0;i<this.length;i++){
		if(this[i][key]==value){
			return i;
		}
	}
	return -1;
}